package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.pojo.*;
import com.qf.service.*;
import com.qf.util.JacksonUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminShopController {
    @Autowired
    private DstShopService dstShopService;
    @Autowired
    private DtsAdminService dtsAdminService;
    @Autowired
    private DtsRoleService dtsRoleService;
    @Autowired
    private DtsCategoryService dtsCategoryService;


    //显示区域列表--分页
    @GetMapping("/region/list")
    public Object list(int page,int limit,String sort,String order){

        //获得佣金表集合  指定范围，页数以及每一页多少条
        List<DtsRegion> dtsRegions = dstShopService.findRegion(page, limit, sort, order);
        //total总数
        Integer count = dstShopService.regionCount();
        Map<String,Object> result =new HashMap<>();

        result.put("total",count);
        result.put("items",dtsRegions);
        return ResponseUtil.ok(result);
    }
    //显示品牌制造商列表--分页
    @RequestMapping("/brand/list")
    public Object brandlist(int page,int limit,String sort,String order,Integer id,String name){


        IPage<DtsBrand> brand = dstShopService.findBrand(page, limit, sort, order, id, name);
        Map<String,Object> result =new HashMap<>();

        result.put("total",brand.getTotal());
        result.put("items",brand.getRecords());
        return ResponseUtil.ok(result);
    }
    //品牌和管理员列表
    @RequestMapping("/brand/catAndAdmin")
    public Object catAndAdminlist(){
        //获得所有管理员
        List<Map<String, Object>> admins = dtsRoleService.options1();
        //获得所有商品类别
        List<CatVo> dtsCategory = dstShopService.findCatvo();
        Map<String,Object> result =new HashMap<>();

        result.put("adminList",admins);
        result.put("categoryList",dtsCategory);
        return ResponseUtil.ok(result);
    }


    /**
     *
     * {
     * 	"address": "自提地址",
     * 	"adminId": 1,
     * 	"categoryIds": [
     * 		100101355,
     * 		100101354
     * 	],
     * 	"defaultCategoryId": 100101354,
     * 	"desc": "介绍介绍",
     * 	"fetchTimeRules": [
     * 		"11:21 - 12:23",
     * 		"10:23 - 12:25"
     * 	],
     * 	"floorPrice": "20000",
     * 	"latitude": "11",
     * 	"longitude": "11",
     * 	"name": "千锋好程序员",
     * 	"picUrl": "https://juhuixing-public.oss-cn-shenzhen.aliyuncs.com/rupcy6hw84r2kcvvpxnf.jpg"
     * }
     *
     *
     *
     * {下行数据
     * 	"errno": 0,
     * 	"data": {
     * 		"id": 1001079,
     * 		"name": "京东热不热",
     * 		"desc": "京东到底热不热呢",
     * 		"floorPrice": 1000,
     * 		"addTime": "2021-05-19 17:33:06",
     * 		"updateTime": "2021-05-19 17:33:06",
     * 		"shareUrl": "",
     * 		"adminId": 6,
     * 		"defaultCategoryId": 100100201
     *        },
     * 	"errmsg": "成功"
     * }
     * //品牌制造商添加http://localhost:8083/admin/brand/create
     */
    @RequestMapping("/brand/create")
    public Object brandcreate(@RequestBody String body){

        String address = JacksonUtil.parseString(body, "address");
        String desc = JacksonUtil.parseString(body, "desc");
        List<Integer> categoryIds = JacksonUtil.parseIntegerList(body, "categoryIds");
        String fetchTimeRules = JacksonUtil.parseString(body, "fetchTimeRules");
        String name = JacksonUtil.parseString(body, "name");
        String latitude = JacksonUtil.parseString(body, "latitude");
        String longitude = JacksonUtil.parseString(body, "longitude");
        String picUrl = JacksonUtil.parseString(body, "picUrl");
        Integer floorPrice = JacksonUtil.parseInteger(body, "floorPrice");
        Integer adminId = JacksonUtil.parseInteger(body, "adminId");
        Integer defaultCategoryId = JacksonUtil.parseInteger(body, "defaultCategoryId");
        DtsBrand dtsBrand=new DtsBrand();
        dtsBrand.setAdminId(adminId);
        dtsBrand.setAddress(address);
        dtsBrand.setDefaultCategoryId(defaultCategoryId);
       dtsBrand.setFloorPrice(new BigDecimal(floorPrice));
//        dtsBrand.setLatitude(latitude);
        dtsBrand.setName(name);
        dtsBrand.setPicUrl(picUrl);
        dtsBrand.setDesc(desc);
        dtsBrand.setFetchTimeRules(fetchTimeRules);
        dstShopService.createbrand(dtsBrand);

        Map<String,Object> result =new HashMap<>();
        result.put("name",dtsBrand.getName());
        result.put("desc",dtsBrand.getDesc());
        result.put("addTime",dtsBrand.getAddTime());
        result.put("updateTime",dtsBrand.getUpdateTime());
        result.put("floorPrice",dtsBrand.getFloorPrice());
        result.put("shareUrl",dtsBrand.getShareUrl());
        result.put("picUrl",dtsBrand.getPicUrl());
        result.put("adminId",dtsBrand.getAdminId());
        result.put("id",dtsBrand.getId());
        result.put("defaultCategoryId",dtsBrand.getDefaultCategoryId());

        return ResponseUtil.ok(result);
    }
    //品牌制造商添加http://localhost:8083/admin/brand/delete
    @RequestMapping("/brand/delete")
    public Object branddelete(@RequestBody String body){
        Integer defaultCategoryId = JacksonUtil.parseInteger(body, "defaultCategoryId");
        Integer id = JacksonUtil.parseInteger(body, "id");
        DtsBrand dtsBrand=new DtsBrand();
        dtsBrand.setDefaultCategoryId(defaultCategoryId);
        dtsBrand.setId(id);
        dstShopService.deletebrand(dtsBrand);
        return ResponseUtil.ok();
    }


        //显示商品类目列表--分页
    @RequestMapping("/category/list")
    public Object goodslist(int page,int limit,String sort,String order,Integer id,String name){


        IPage<DtsCategory> dtsGoods = dtsCategoryService.selectPage(page, limit, sort, order,id, name);


        Map<String,Object> result =new HashMap<>();

        result.put("total",dtsGoods.getTotal());
        result.put("items",dtsGoods.getRecords());
        return ResponseUtil.ok(result);
    }
    //http://localhost:8083/admin/category/l1
    //一级类目列表
    @RequestMapping("/category/l1")
    public Object category1(){
        //获得所有管理员
        List<Map<String, Object>> category1 = dtsCategoryService.options();

        return ResponseUtil.ok(category1);
    }

    /**
     * #####  商品类目添加
     * - 接口地址 : http://localhost:8083/admin/category/create
     */
    @PostMapping("/category/create")
    public Object categorycreat(@RequestBody DtsCategory dtsCategory){
        dtsCategoryService.create(dtsCategory);
        //下行数据
        Map<String,Object> result =new HashMap<>();
        result.put("id",dtsCategory.getId());
        result.put("name",dtsCategory.getName());
        result.put("keywords",dtsCategory.getKeywords());
        result.put("desc",dtsCategory.getDesc());
        result.put("level",dtsCategory.getLevel());
        result.put("addTime",dtsCategory.getAddTime());
        result.put("updateTime",dtsCategory.getUpdateTime());
        return ResponseUtil.ok(result);
    }
    /**
     * #####  商品类目更新/update
     * - 接口地址 : http://localhost:8083/admin/category/create
     */
    @PostMapping("/category/update")
    public Object categoryupdate(@RequestBody DtsCategory dtsCategory){
        dtsCategoryService.update(dtsCategory);

        return ResponseUtil.ok();
    }
    /**
     * #####  商品类目删除
     * - 接口地址 : http://localhost:8083/admin/category/delete
     */
    @PostMapping("/category/delete")
    public Object categorydelect(@RequestBody DtsCategory dtsCategory){
        dtsCategoryService.delete(dtsCategory);

        return ResponseUtil.ok();
    }


}

